package com.gxa.controller.HospitalController;


import com.gxa.entity.DoctorLabel;
import com.gxa.service.DoctorService;
import com.gxa.utils.ImageUpload;
import com.gxa.utils.R;
import com.gxa.vo.DepCampusVo;
import com.gxa.vo.DoctorVo1;
import com.gxa.vo.DoctorVoPic;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@Api(tags="医生管理模块相关接口")
@RestController
public class DoctorController {

    @Autowired
    private DoctorService doctorService;



    @ApiOperation(value="医生头像图片上传接口",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })

    @PostMapping("/DoctorPic")

    public R DoctorPic(@ModelAttribute @Validated DoctorVoPic doctorVo) throws IOException {
        MultipartFile pic = doctorVo.getUrl();//获取传过来的图片文件
        System.out.println("##############################################");
        String url = ImageUpload.upload(pic);//传过来的文件放在工具类里面处理
        System.out.println(url);

        return R.ok().put("data",url);
    }

    @ApiOperation(value="无参查询接口",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("doc:sel")
    @PostMapping("/doc/QueryDoctorsAll")
    public R QueryDoctorsAll(){
        List<DoctorVo1> doctorVos = this.doctorService.queryAll();
        System.out.println(doctorVos);
        for (int i = 0; i < doctorVos.size(); i++) {
            doctorVos.get(i).setLabel(this.doctorService.queryLabelById(doctorVos.get(i).getId()));

        }

        System.out.println(doctorVos);
        return R.ok().put("data",doctorVos);

    }

    @ApiOperation(value="查询前科室下拉框",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("doc:sel")
    @PostMapping("/doc/QueryDoctors1")
    public R QueryDoctors1(){

        List<String> list = this.doctorService.query1();

        System.out.println(list);
        return R.ok().put("data",list);

    }

    @ApiOperation(value="查询接口",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("doc:sel")
    @PostMapping("/doc/QueryDoctors")
    public R QueryDoctors(@ApiParam(name="医生姓名",value = "根据医生姓名查询",required = true)@RequestBody DoctorVo1 doctorVo1){

//        List<DoctorVo1> doctorVos = this.doctorService.queryAll();
//        System.out.println(doctorVos);
//        for (int i = 0; i < doctorVos.size(); i++) {
//            doctorVos.get(i).setLabel(this.doctorService.queryLabelById(doctorVos.get(i).getId()));
//
//        }

        List<DoctorVo1> doctorVos = this.doctorService.queryByName(doctorVo1);
        System.out.println(doctorVos);
        for (int i = 0; i < doctorVos.size(); i++) {
            doctorVos.get(i).setLabel(this.doctorService.queryLabelById(doctorVos.get(i).getId()));

        }
        System.out.println(doctorVos);
        return R.ok().put("data",doctorVos);

    }
    @ApiOperation(value="修改前查询接口",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("doc:sel")
    @PostMapping("/doc/PreEditDoctors")
    public R PreEditDoctors(@ApiParam(name="医生姓名",value = "根据医生名查询",required = true)@RequestBody DoctorVo1 doctorVo1){

        int id = this.doctorService.queryId(doctorVo1);
        doctorVo1.setId(id);


        System.out.println(doctorVo1);
        return R.ok().put("data",doctorVo1);

    }

    @ApiOperation(value="修改/添加前查询科室下拉列表",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("doc:sel")
    @PostMapping("/doc/PreEditDoctorQueryDept")
    public R PreEditDoctorQueryDept(){
        List<DepCampusVo> list = this.doctorService.query2();

        for (int i = 0; i < list.size(); i++) {

            System.out.println(list.get(i));
        }
        System.out.println("########################"+list);
        return R.ok().put("data",list);

    }

//    @ApiOperation(value="修改/添加前查询院区下拉列表",notes="方法的详细描述",produces = "application/json",response = R.class)
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
//    })
//    @ApiResponses(value = {
//            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
//                    value = {
//                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
//                    })),
//            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
//            @ApiResponse(code = 307, message = "令牌格式不正确"),
//            @ApiResponse(code = 401, message = "没有权限"),
//            @ApiResponse(code = 400, message = "请求错误"),
//            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
//            @ApiResponse(code = 500, message = "服务器内部错误")
//    })
//    @RequiresPermissions("doc:sel")
//    @PostMapping("/doc/PreEditDoctorQueryCampus")
//    public R PreEditDoctorQueryCampus(){
//        List<String> list = this.doctorService.query1();
//
//        System.out.println(list);
//        return R.ok().put("data",list);
//
//    }

    @ApiOperation(value="修改/添加前查询职称下拉列表",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("doc:sel")
    @PostMapping("/doc/PreEditDoctorQueryProf")
    public R PreEditDoctorQueryProf(){
        List<String> list = this.doctorService.queryProf();

        System.out.println(list);
        return R.ok().put("data",list);

    }

    @ApiOperation(value="修改/添加前查询标签下拉列表",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("doc:sel")
    @PostMapping("/doc/PreEditDoctorQueryLabel")
    public R PreEditDoctorQueryLabel(){
        List<String> list = this.doctorService.queryLabel();

        System.out.println(list);
        return R.ok().put("data",list);

    }

    @ApiOperation(value="修改接口",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("doc:update")
    @PutMapping("/doc/EditDoctors")
    public R EditDoctors(@RequestBody DoctorVo1 doctorVo){

        int departmentId = this.doctorService.queryDepartmentId(doctorVo.getDepartment());
        doctorVo.setDepartmentId(departmentId);
        this.doctorService.updateDoctor(doctorVo);

        DoctorLabel doctorLabel = new DoctorLabel();
        for (int i = 0; i < doctorVo.getLabel().size(); i++) {
            int labelId = this.doctorService.queryLabelId(doctorVo.getLabel().get(i));
            doctorLabel.setLabelId(labelId);
            doctorLabel.setDoctorId(doctorVo.getId());
            doctorLabel.setId(this.doctorService.queryDLId(doctorLabel));
            this.doctorService.updateDoctorLabel(doctorLabel);
            System.out.println(doctorLabel);
        }

        return R.ok();

    }



    @ApiOperation(value="添加接口",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("doc:add")
    @PostMapping("/doc/addDoctors")
    public R addDoctors(@ApiParam(name="医生",value = "医生参数",required = true)@RequestBody DoctorVo1 doctorVo){

        int departmentId = this.doctorService.queryDepartmentId(doctorVo.getDepartment());
        doctorVo.setDepartmentId(departmentId);
        this.doctorService.insertDoctor(doctorVo);
        int id = this.doctorService.queryId1(doctorVo);


        DoctorLabel doctorLabel = new DoctorLabel();
        for (int i = 0; i < doctorVo.getLabel().size(); i++) {
            int labelId = this.doctorService.queryLabelId(doctorVo.getLabel().get(i));
            doctorLabel.setLabelId(labelId);
            doctorLabel.setDoctorId(id);
//            int dlId = this.doctorService.queryDLId(doctorLabel);
//            doctorLabel.setId(dlId);
            this.doctorService.insertDoctorLabel(doctorLabel);
            System.out.println(doctorLabel);
        }
        System.out.println(doctorVo);
        return R.ok();

    }

    @ApiOperation(value="删除接口",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
//    @RequiresPermissions("doc:del")
    @DeleteMapping("/doc/DelDoctors")
    public R DelDoctors(@ApiParam(name="医生数据",value = "根据医生数据删除",required = true)@RequestBody DoctorVo1 doctorVo){

        int id = this.doctorService.queryId1(doctorVo);

        this.doctorService.DelDoctor(id);

        this.doctorService.DeleteDoctorLabel(id);

        System.out.println(doctorVo);

        return R.ok();

    }
}
